package com.example.atools;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import android.os.Environment;

public class SDCardTools {
	private static String mPullFileName="PullSettings.txt";
	public static boolean writePullToSDCard(String sValue) {
		try
        {
            File file = new File(Environment.getExternalStorageDirectory(),mPullFileName);
            BufferedWriter writer = new BufferedWriter(new FileWriter(file));
            writer.write(sValue);
            writer.close();
            return true;
        } catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
		return false;
	}

	public static String readPullFileFromSDCard() {
	    File directory = Environment.getExternalStorageDirectory();
	    // Assumes that a file article.rss is available on the SD card
	    File file = new File(directory + "/" + mPullFileName);
	    if (!file.exists()) {
	        //throw new RuntimeException("File not found");
	    	return null;
	    }
	    //Log.e("Testing", "Starting to read");
	    BufferedReader reader = null;
	    StringBuilder builder=null;
	    try {
	        reader = new BufferedReader(new FileReader(file));
	        builder = new StringBuilder();
	        String line;
	        while ((line = reader.readLine()) != null) {
	            builder.append(line);
	        }
	    } catch (Exception e) {
	        e.printStackTrace();
	    } finally {
	        if (reader != null) {
	            try {
	                reader.close();
	            } catch (IOException e) {
	                e.printStackTrace();
	            }
	        }
	    }
	    if (builder !=null)
	    	return builder.toString();
	    return null;
	}	
	
}
